package interview5;

/**
 * @author Jack
 * @date 2019-11-02-14:55
 * 面试题5相关题目:将两个有序的数组合并
 */
public class Solution2 {

    public static void main(String[] args) {
        int[] arr1 = new int[10];
        arr1[0] = 1;
        arr1[1] = 4;
        arr1[2] = 5;
        arr1[3] = 7;
        arr1[4] = 9;
        int[] arr2 = new int[]{10,11,12,13,15};
        int[] result = insertToArr1(arr1, arr2);
        for (int i : result) {
            System.out.print(i+" ");
        }
    }

    public static int[] insertToArr1(int[] arr1, int[] arr2){
        if(arr1 == null || arr1.length == 0){
            return null;
        }
        if(arr2 == null || arr2.length == 0){
            return arr1;
        }
        int p1 = arr1.length - arr2.length - 1;
        int p2 = arr1.length - 1;
        int index = arr2.length - 1;
        while (p1 >= 0 && index >= 0){
            if(arr2[index] >= arr1[p1]){
                arr1[p2] = arr2[index];
                index--;
                p2--;
                continue;
            }
            arr1[p2] = arr1[p1];
            p2--;
            p1--;
        }
        return arr1;
    }

}
